﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BugTracker.Models;
using BugTracker.Helper;

namespace BugTracker.Controllers
{
    public class DeveloperController : Controller
    {
        //
        // GET: /Developer/

        Bug_db db = new Bug_db();
        SessionHelper sessionhelper = new SessionHelper();

        public ActionResult Home()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View();
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //View project table
        public ActionResult Project()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View(db.Projects.ToList());
                }
            }
             return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //View Project Details
        public ActionResult Details(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Project projects = db.Projects.Find(id);
                    return View(projects);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Edit project
        public ActionResult Edit(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Project project = db.Projects.Find(id);
                    ViewData["Status"] = (db.Status.OrderBy(r => r.status_id)).ToList();
                    return View(project);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }
      
        public ActionResult DetailsIssue(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Issue issue = db.Issues.Find(id);
                    return View(issue);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        [HttpPost]
        public ActionResult Edit(Project projects)
        {
            if (ModelState.IsValid)
            {
                db.Entry(projects).State = EntityState.Modified;
                projects.project_idPK = projects.project_idPK;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(projects);
        }

       public ActionResult Issue()
        {
           Response.Cache.SetCacheability(HttpCacheability.NoCache);
           Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
           Response.Cache.SetNoStore();

           if (sessionhelper.Session_Info() != null)
           {
               if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
               {
                   return RedirectToAction("Login", "Home", new { msg = "expired" });
               }
               else
               {
                   return View(db.Issues.ToList());
               }
           }
           return RedirectToAction("Login", "Home", new { msg = "expired" });

        }
    }
}